package com.topwater.topwater.controller;

import com.hp.hpl.sparta.xpath.Step;
import com.topwater.topwater.entity.BeHistory;
import com.topwater.topwater.entity.BeOrder;
import com.topwater.topwater.entity.BeOrderuser;
import com.topwater.topwater.entity.UsMeter;
import com.topwater.topwater.service.BeHistoryService;
import com.topwater.topwater.service.BeOrderService;
import com.topwater.topwater.service.BeOrderuserService;
import com.topwater.topwater.utils.Rconfig;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.topwater.topwater.utils.ResponseData;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (BeOrder)表控制层
 *
 * @author 胡文
 * @since 2023-08-23 16:32:01 生成
 */
@RestController
@RequestMapping("beOrder")
@Api(tags = "BeOrder模块接口说明")
public class BeOrderController {
    /**
     * 服务对象
     */
    @Autowired
    private BeOrderService beOrderService;
    @Autowired
    private BeOrderuserService beOrderuserService;
    @Autowired
    private BeHistoryService beHistoryService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过id查询BeOrder数据的方法")
    @GetMapping(value = "{id}", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryById(@PathVariable("id") String id) {
        return ResponseData.ok().putDataValue("obj", this.beOrderService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param beOrder 实体
     * @return 新增结果
     */
    @Operation(summary = "添加BeOrder表")
    @PostMapping(value = "add", produces = {"application/json;charset=UTF-8"})
    public ResponseData add(BeOrder beOrder) {
        return ResponseData.ok().putDataValue("obj", this.beOrderService.insert(beOrder));
    }

    /**
     * 新增数据
     * @param beOrder 实体
     * @return 新增结果
     */
    @Operation(summary = "添加BeOrder表")
    @PostMapping(value = "addNotNull", produces = {"application/json;charset=UTF-8"})
    public ResponseData addNotNull(BeOrder beOrder, HttpServletRequest request ){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        String sa = Rconfig.generateRandom(4);
        String format = formatter.format(date);
        String oldorderno="B1-" + format + "-" + sa;
        if (beOrder.getOlduserno()==null || beOrder.getOlduserno()=="") {
            String s = Rconfig.generateRandom(10);
            SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM-dd");
            Date dates = new Date(System.currentTimeMillis());
            beOrder.setOrderno("B1-" + format + "-" + sa);
            beOrder.setCreateemp(1);
            beOrder.setCreatedate(dates);
            beOrder.setLasteditemp(1);
            beOrder.setLasteditdate(dates);
            beOrder.setOrdertype(1);
            beOrder.setStepid(1);
            BeHistory beHistory = new BeHistory();
            beHistory.setOrderno(beOrder.getOrderno());
            beHistory.setStepid(1);
            beHistory.setDatee(dates);
            beHistory.setEmpid(1);
            beHistory.setIsback(1);
            beHistoryService.insertNotNull(beHistory);
            request.getSession().setAttribute("Orderno", beOrder.getOrderno());
            return ResponseData.ok().putDataValue("obj", this.beOrderService.insertNotNull(beOrder));
        }else{
            System.out.println("beOrder = " + beOrder);
            Object attribute = request.getSession().getAttribute("Orderno");
            beOrder.setOrderno((String) attribute);
            beOrder.setOlduserno("");
          return ResponseData.ok().putDataValue("obj", this.beOrderService.update(beOrder));
        }
    }

    /**
     * 编辑数据
     *
     * @param beOrder 实体
     * @return 编辑结果
     */
    @Operation(summary = "修改BeOrder表,根据id修改")
    @PostMapping(value = "update", produces = {"application/json;charset=UTF-8"})
    public ResponseData update(BeOrder beOrder) {
        System.out.println("beOrder = " + beOrder);
        SimpleDateFormat formatters= new SimpleDateFormat("yyyy-MM-dd");
        Date dates = new Date(System.currentTimeMillis());
        beOrder.setAuditempid(1);
        beOrder.setLasteditdate(dates);
        beOrder.setStepid(2);
        BeHistory beHistory=new BeHistory();
        beHistory.setOrderno(beOrder.getOrderno());
        beHistory.setStepid(2);
        beHistory.setDatee(dates);
        beHistory.setEmpid(1);
        beHistory.setIsback(1);
        beHistoryService.insertNotNull(beHistory);
        return ResponseData.ok().putDataValue("obj", this.beOrderService.update(beOrder));
    }
    @Operation(summary = "修改BeOrder表,根据id修改")
    @PostMapping(value = "updates", produces = {"application/json;charset=UTF-8"})
    public ResponseData updates(BeOrder beOrder) {
        System.out.println("beOrder = " + beOrder);
        return ResponseData.ok().putDataValue("obj", this.beOrderService.update(beOrder));
    }
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Operation(summary = "删除BeOrder,根据id删除")
    @PostMapping(value = "deleteById", produces = {"application/json;charset=UTF-8"})
    public ResponseData deleteById(String id) {
        System.out.println("id = " + id);
        return ResponseData.ok().putDataValue("obj", this.beOrderService.deleteById(id));
    }

    //单字符串全部模糊查询 带分页
    @Operation(summary = "模糊查询BeOrder 分页,一个字符串模糊查所有列")
    @GetMapping(value = "queryByLike2", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryByLike2(String str,
                                     @ApiParam("第几页")
                                     @RequestParam(defaultValue = "1", required = false) int pageNum,
                                     @ApiParam("每页多少条")
                                     @RequestParam(defaultValue = "10", required = false) int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<BeOrder> list = this.beOrderService.queryByLike2(str);
        PageInfo pageInfo = new PageInfo<BeOrder>(list, 7);
        return ResponseData.ok().putDataValue("pageInfo", pageInfo);
    }
    //单字符串全部模糊查询 带分页
    @Operation(summary = "模糊查询BeOrder 分页,一个字符串模糊查所有列")
    @GetMapping(value = "queryByLike3", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryByLike3(String str,
                                     @ApiParam("第几页")
                                     @RequestParam(defaultValue = "1", required = false) int pageNum,
                                     @ApiParam("每页多少条")
                                     @RequestParam(defaultValue = "10", required = false) int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<BeOrder> list = this.beOrderService.queryByLike3(str);
        PageInfo pageInfo = new PageInfo<BeOrder>(list, 7);
        return ResponseData.ok().putDataValue("pageInfo", pageInfo);
    }

    //带分页的模糊查询
    @Operation(summary = "带分页的模糊查询BeOrder ,主要用这个即可")
    @GetMapping(value = "queryByLike", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryByLike(BeOrder beOrder,
                                    @ApiParam("第几页")
                                    @RequestParam(defaultValue = "1", required = false) int pageNum,
                                    @ApiParam("每页多少条")
                                    @RequestParam(defaultValue = "10", required = false) int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<BeOrder> list = this.beOrderService.queryByLike(beOrder);
        PageInfo pageInfo = new PageInfo<BeOrder>(list, 7);
        return ResponseData.ok().putDataValue("pageInfo", pageInfo);
    }

}

